#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "../include/nary.h"

/*void main()
{
	//struct node* naryroot=NULL;
	struct node* temp;
	struct node* t;
	struct node* m;
	fd* root=(fd*)malloc(sizeof(fd));
	//printf("after t");
	strcpy(root->filename,"home");
	//printf("%s",root->filename);
	strcpy(root->path,"/home");
	//printf("%s",root->path);
	fd* new=(fd*)malloc(sizeof(fd));
	strcpy(new->filename,"dir1");
	strcpy(new->path,"/home/dir1");
	temp=malloc(sizeof(struct node));
	temp->f=new;
	temp->left=NULL;
	temp->right=NULL;
	naryroot=insert_nary(root);
 	naryroot=insert_nary(new);
	//printf("\n %s",new->path);
	m=(struct node*)malloc(sizeof(struct node));
	m->f=root;
	m->left=temp;
	m->right=NULL;

		
	fd* new1=(fd*)malloc(sizeof(fd));
	strcpy(new1->filename,"dir2");
	strcpy(new1->path,"/home/dir2");
	//printf("\n %s",new1->path);
      	naryroot=insert_nary(new1);
	
	fd* new2=(fd*)malloc(sizeof(fd));
	strcpy(new2->filename,"dir3");
	strcpy(new2->path,"/home/dir3");
	//printf("\n %s",new1->path);
      	naryroot=insert_nary(new2);
 
	fd* new3=(fd*)malloc(sizeof(fd));
	strcpy(new3->filename,"dir4");
	strcpy(new3->path,"/home/dir2/dir4");
	//printf("\n %s",new1->path);
      	naryroot=insert_nary(new3);
	//t=(struct node*)malloc(sizeof(struct node));

 
	fd* new4=(fd*)malloc(sizeof(fd));
	strcpy(new4->filename,"dir5");
	strcpy(new4->path,"/home/dir1/dir5");
	//printf("\n %s",new1->path);
      	naryroot=insert_nary(new4);
	
 	//t=insert(new);
	//printf("back in main");
 	//printf("\n %s",t->f->path);
	//printf("\n");
	display_nary(naryroot);
	printf("\n before delete\n");
	delete_nary(new4);
	printf("\n after delete\n");
	display_nary(naryroot);
        printf("\n before 2nd delete\n");
        delete_nary(new4);
        printf("\n after 2nd delete\n");
		

}*/

struct node* insert_nary(fd* x)
{

	struct node* root;
	root=naryroot;
	struct node* temp;
	struct node* t;
	temp=(struct node*)malloc(sizeof(struct node));
	temp->f=x;
	temp->left=NULL;
	temp->right=NULL;
	
	if(root==NULL)
	{
		root=(struct node*)malloc(sizeof(struct node));
	
		root=temp;
	
		return root;
		
	}
       	if(root->left==NULL)
	{
		root->left=temp;
		return root;
		
	}
	else
	{
	t=root->left;
	int flag=1;
	
	while(flag)
	{

		if((strstr(temp->f->path,t->f->path))!=NULL && (strcmp(temp->f->path,t->f->path)!=0))
		{
 			 if(t->left)
    				 t=t->left;
 			 else
    				{
				t->left=temp;
				check=1;				
				//printf("\n\n inserted as left child");
				}
		}

 		else if(strcmp(temp->f->path,t->f->path)==0)
		{
			flag=0;
			//printf("\n2222222222NARY........ File already exists");
			if (check==0)
			return NULL;
			
		}

		else
		{
			 if(t->right)
   			 
     				t=t->right;
  			else
				{
     				t->right=temp;
			        flag=0;
				check=1;
					
				}
    		}
	}
	return root;    
  	}
}

void display_nary(struct node* root){
	if(root==NULL)
		//printf("\nNo files to display..\n");
	if(root){
		//printf("\n%s\n",root->f->filename);
		if(root->left)
			display_nary(root->left);
		if(root->right)
			display_nary(root->right);
	}
}

void initialize_root(fd *fd1){
    naryroot=(struct node *)malloc(sizeof(struct node));
    naryroot->f=fd1;
    naryroot->left=NULL;
    naryroot->right=NULL;
   
    //return 1;

}
void nary_write(struct node* root,FILE *fp){
       // printf("3333Inside nary_write....%s\n",(root->f)->path);
       /* 
        if(fp==NULL)
          //printf("\nFILE POINTER INSIDE NARY_WRITE IS NULL.\n");
	if(root==NULL)
		printf("\nUnmount Error...Nothing to save\n");
	*/
	if(root){
	//if(fp==NULL){
               // printf("22222222%s\n",(root->f)->path);
		fwrite((root->f),sizeof(fd),1,fp);
		
		if(root->left)
			nary_write(root->left,fp);
		if(root->right)
			nary_write(root->right,fp);
	}
}

int delete_nary(fd *filedes){

  struct node *t1, *t;
  /*
  if(naryroot == NULL){
   //printf("Cannot delete from an empty nary tree!\n");
   return 0;
  }
  
  if(strcmp(filedes->path, naryroot->f->path)==0){
   printf("Cannot delete the root directory!\n");
   return 0;
  }
*/
  t1 = naryroot;
  
  if(t1->left)
    t = t1->left;
 
  int flag=1;
  while(flag)
	{

		if((strstr(filedes->path,t->f->path))!=NULL && (strcmp(filedes->path,t->f->path)!=0))
		{
 			 if(t->left){
 			    t1 = t;
    			    t = t->left;
			  }
  				
 			 
		}

 		else if(strcmp(filedes->path,t->f->path)==0)
		{
			flag=0;
			
  			if(t->left){   //Cannot delete directory which has contents.
                         //printf("Directory is not empty.\n");
		         return 0;
  			}
                 
                        else{
                         if(t1->left == t)
                            t1->left = t->right;
			 else   //t1->right = t
                            t1->right = t->right;
			}
			
		}

		else
		{
			 if(t->right){
   			    t1 = t;
     			    t = t->right;
			  }

  			
    		}
	}
  


}

struct node* find_node(char source_dir_path[])
{	struct node *curr=NULL;
	curr = naryroot;
	int status;
	struct node *parent;
	
		status=FALSE;
		parent=naryroot;
		while(status==FALSE)
		{	
			if(strstr(source_dir_path,parent->f->path)==NULL)
			{	parent=parent->right;
				curr = parent;	
			}
			else
			{	if(strcmp(source_dir_path,parent->f->path)==0)
					return curr;
				else
				{	parent=parent->left;
					curr = parent;
				}
			}
		}
}



struct node* searchn(struct node* root,char path1[]) {
	
	if(root != NULL) {
		
		if(strcmp(root->f->path,path1)==0)
                        return root;
		if(root->left)
                	searchn(root->left,path1);
		if(root->right)	                
			searchn(root->right,path1);

	} 	
}

void nonRecursive(struct node* root,FILE *fp1)
{
    
   	
        if(root!=NULL)
        {
		//printf("%s",root->f->filename);
		//printf("%s",*fp1);
		fprintf(fp1,"%s\n",root->f->filename);
		//fputs(root->f->filename,fp1);
		nonRecursive(root->right,fp1);
        }
}

void Recursive(struct node* root,FILE *fp1)
{
    //  printf("\n Inside recursive 1111111111111111111");
  
      //fprintf(fp1,"%s\n",root->f->filename);
      if(root!=NULL){
		
		//display_nary(root);
		
		
		fprintf(fp1,"%s\n",root->f->filename);
        	
		//printf("%s\n",root->f->filename);
                
		if(root-> left){
			Recursive(root->left,fp1);
			
		}
		if(root->right){ 
			
			Recursive(root->right,fp1);
			
		}
        }
}


struct node *source_node_remove(char source_dir_path[])
{
	struct node *parent;	
	struct node *curr=NULL, *prev=NULL;
	curr = naryroot;
	int s;
	s=FALSE;
	parent=naryroot;
	while(s==FALSE)
	{	
			if(strstr(source_dir_path,parent->f->path)==NULL)
			{	
				if(parent->right == NULL)
					s=TRUE;
				else
				{	prev = parent;
					parent=parent->right;
					curr = parent;
				}
			}
			else
			{	
				if(parent->left==NULL)
					s=TRUE;	
				else
				{	if(strcmp(source_dir_path,parent->f->path)==0)
						s=TRUE;
					else
					{	prev = parent;
						parent=parent->left;
						curr = parent;
					}
				}	
			}
		}
		if(prev->left == curr)
			prev->left = curr->right;
		else if(prev->right == curr)
			prev->right = curr->right;
		curr->right = NULL;
	
return curr;

}

struct node* des_node_remove(char source_dir_path[])
{	struct node *curr=NULL;
	curr = naryroot;
	int s;
	struct node *parent;
	
		s=FALSE;
		parent=naryroot;
		while(s==FALSE)
		{	
			if(strstr(source_dir_path,parent->f->path)==NULL)
			{	parent=parent->right;
				curr = parent;	
			}
			else
			{	if(strcmp(source_dir_path,parent->f->path)==0)
					return curr;
				else
				{	parent=parent->left;
					curr = parent;
				}
			}
		}
}

void destnode_moved(struct node *srcroot, char dest_dir_path[])
{	
	struct node *desRoot;
	desRoot = des_node_remove(dest_dir_path);	
	srcroot->right = desRoot->left;
	desRoot->left = srcroot;
}

void set_newpath(struct node *srcroot, char dest_dir_path[])
{	char newPath[100];
	if(srcroot != NULL)
	{	strcpy(newPath, dest_dir_path);
		strcat(newPath, "/");
		strcat(newPath, srcroot->f->filename);
		strcpy(srcroot->f->path, newPath);
		//printf("\nname: %s\tpath: %s\n",srcroot->f->filename, srcroot->f->path);
		set_newpath(srcroot->right, dest_dir_path);	
		set_newpath(srcroot->left, newPath);
	}
}

char* find_dirname(char *path)
{
	int l,i;
	int j=0,r=1,k=0;
	char *temp;
	char tok[10][10];
	temp=(char*)malloc(sizeof(10));			
	l=strlen(path);
	for(i=1;i<l;i++)
	{		
		if(r==1)
		{
			k=0;
			r=0;
		}	
		if(path[i]=='/')	
		{
			*(temp+k)='\0';
			strcpy(tok[j],temp);		
			j++;
			r=1;
		}
		else
		{
			*(temp+k)=path[i];	
			k++;
		}
	}
	*(temp+k)='\0';
	return temp;
}
